package middlware;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class ServerRequestHandler {
	
	ServerSocket s = null;
	
	
	public static void main(String[] args) throws UnknownHostException, IOException {
			@SuppressWarnings("unused")
			ServerRequestHandler srh = new ServerRequestHandler();
	}
	
	public ServerRequestHandler() throws UnknownHostException, IOException {
		this.s = new ServerSocket(1500);
		aguardarClienteConectar();
	}
	
	public void aguardarClienteConectar() throws IOException{
		while (true) {
			System.out.print("Aguardando o servico ser solicitado...");
			Socket conexao = s.accept();
			System.out.println(" Solicitou!");
			@SuppressWarnings("unused")
			ManipulaConexao mc = new ManipulaConexao(conexao);
		}
	}
	
	
	
	class ManipulaConexao implements Runnable {
		private Socket conexao;

		public ManipulaConexao(Socket conexao) {
			this.conexao = conexao;
			Thread t = new Thread(this);
			t.start();
		}
		
		
		public void run() {
			
			//mensagens devem ser serializadas
			byte[] requisicao = null;
			byte[] msgResposta = null;

			ObjectInputStream  receiveClientRequestHandler = null;
			ObjectOutputStream outToClientRequestHandler   = null;
			

			//recebe requisicao e envia pra o invoker
				try {
					 receiveClientRequestHandler = new ObjectInputStream(conexao.getInputStream());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					requisicao = (byte[])  receiveClientRequestHandler.readObject();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Invoker invoker = new Invoker();
				try {
					msgResposta = invoker.recebeRequisicaoServerRequestHandler(requisicao);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			//recebe e envia pra o clienteRequestHandler	
				try {
					outToClientRequestHandler = new ObjectOutputStream(conexao.getOutputStream());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					outToClientRequestHandler.writeObject(msgResposta);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					conexao.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
	}
	
}
